package chapter15.exercise;

import java.util.Iterator;

public class IterableFibonacci extends Fibonacci implements Iterable<Integer> {
    private int n;

    public IterableFibonacci(int n) {
        this.n = n;
    }

    class FibonacciIterator implements Iterator<Integer> {
        public boolean hasNext() {
            return n > 0;
        }

        public Integer next() {
            n--;
            return IterableFibonacci.this.next();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

    public Iterator<Integer> iterator() {
        return new FibonacciIterator();
    }

    public static void main(String[] args) {
        IterableFibonacci fibonacci = new IterableFibonacci(40);
        Iterator<Integer> iterator = fibonacci.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
    }
}
